package com.sxu.fighting.day2;

/**
 * 125 判断回文串
 */
public class Test1 {
    // 翻转字符串，判断前后是否相等
    public boolean isPalindrome(String s) {
        StringBuffer sb = new StringBuffer();
        int len = s.length();
        for(int i = 0;i<len;i++){
            char c = s.charAt(i);
            if(Character.isLetterOrDigit(c)){
                sb.append(Character.toLowerCase(c));
            }
        }
        StringBuffer sb_rev = new StringBuffer(sb).reverse();
        return sb.toString().equals(sb_rev.toString());
    }

    // 双指针
    public boolean isPalindrome2(String s) {
        StringBuffer sb = new StringBuffer();
        int len = s.length();
        for(int i = 0;i<len;i++){
            char c = s.charAt(i);
            if(Character.isLetterOrDigit(c)){
                sb.append(Character.toLowerCase(c));
            }
        }
        // 使用双指针
        int n = sb.length();
        int left = 0,right = n-1;
        while(left < right){
            if(Character.toLowerCase(sb.charAt(left))!= Character.toLowerCase(sb.charAt(right))){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
